﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK.Graphics.OpenGL;

namespace DataModel.Shapes
{
    public class Cube : SimpleShape
    {
        public Cube(float width, float height, float depth, System.Drawing.Color color, float x, float y, float z) :
            base(width, height, depth, color, x, y, z)
        {
            this.Vertices = new List<Point3D>();

            UpdateVertices(x, y, z);
        }

        public override void UpdateVertices(float x, float y, float z)
        {
            this.X = x;
            this.Y = y;
            this.Z = z;

            if (this.Vertices == null)
                return;

            this.Vertices.Clear();

            //up - LB, RB, RF, LF
            this.Vertices.Add(new Point3D((-Width / 2) + X, (-Height / 2) + Y, (-Depth / 2) + Z));
            this.Vertices.Add(new Point3D((Width / 2) + X, (-Height / 2) + Y, (-Depth / 2) + Z));
            this.Vertices.Add(new Point3D((Width / 2) + X, (-Height / 2) + Y, (Depth / 2) + Z));
            this.Vertices.Add(new Point3D((-Width / 2) + X, (-Height / 2) + Y, (Depth / 2) + Z));
            //bottom - LB, RB, RF, LF
            this.Vertices.Add(new Point3D((-Width / 2) + X, (Height / 2) + Y, (-Depth / 2) + Z));
            this.Vertices.Add(new Point3D((Width / 2) + X, (Height / 2) + Y, (-Depth / 2) + Z));
            this.Vertices.Add(new Point3D((Width / 2) + X, (Height / 2) + Y, (Depth / 2) + Z));
            this.Vertices.Add(new Point3D((-Width / 2) + X, (Height / 2) + Y, (Depth / 2) + Z));
        }

        public override void DrawShape()
        {
            GL.Begin(BeginMode.Quads);
                        
            //up
            GL.Color3(base.ColorBottom);
            GL.Vertex3(this.Vertices[0].X, this.Vertices[0].Y, this.Vertices[0].Z);
            GL.Vertex3(this.Vertices[3].X, this.Vertices[3].Y, this.Vertices[3].Z);
            GL.Vertex3(this.Vertices[2].X, this.Vertices[2].Y, this.Vertices[2].Z);
            GL.Vertex3(this.Vertices[1].X, this.Vertices[1].Y, this.Vertices[1].Z);

            GL.Color3(base.Color);

            //front wall
            GL.Vertex3(this.Vertices[3].X, this.Vertices[3].Y, this.Vertices[3].Z);
            GL.Vertex3(this.Vertices[7].X, this.Vertices[7].Y, this.Vertices[7].Z);
            GL.Vertex3(this.Vertices[6].X, this.Vertices[6].Y, this.Vertices[6].Z);
            GL.Vertex3(this.Vertices[2].X, this.Vertices[2].Y, this.Vertices[2].Z);
            
            //right side
            GL.Vertex3(this.Vertices[1].X, this.Vertices[1].Y, this.Vertices[1].Z);
            GL.Vertex3(this.Vertices[2].X, this.Vertices[2].Y, this.Vertices[2].Z);
            GL.Vertex3(this.Vertices[6].X, this.Vertices[6].Y, this.Vertices[6].Z);
            GL.Vertex3(this.Vertices[5].X, this.Vertices[5].Y, this.Vertices[5].Z);

            //back wall
            GL.Vertex3(this.Vertices[0].X, this.Vertices[0].Y, this.Vertices[0].Z);
            GL.Vertex3(this.Vertices[5].X, this.Vertices[5].Y, this.Vertices[5].Z);
            GL.Vertex3(this.Vertices[4].X, this.Vertices[4].Y, this.Vertices[4].Z);
            GL.Vertex3(this.Vertices[1].X, this.Vertices[1].Y, this.Vertices[1].Z);
            
            //left side
            GL.Vertex3(this.Vertices[0].X, this.Vertices[0].Y, this.Vertices[0].Z);
            GL.Vertex3(this.Vertices[3].X, this.Vertices[3].Y, this.Vertices[3].Z);
            GL.Vertex3(this.Vertices[7].X, this.Vertices[7].Y, this.Vertices[7].Z);
            GL.Vertex3(this.Vertices[4].X, this.Vertices[4].Y, this.Vertices[4].Z);
            
            //bottom
            GL.Color3(base.ColorBottom);
            GL.Vertex3(this.Vertices[4].X, this.Vertices[4].Y, this.Vertices[4].Z);
            GL.Vertex3(this.Vertices[7].X, this.Vertices[7].Y, this.Vertices[7].Z);
            GL.Vertex3(this.Vertices[6].X, this.Vertices[6].Y, this.Vertices[6].Z);
            GL.Vertex3(this.Vertices[5].X, this.Vertices[5].Y, this.Vertices[5].Z);

            GL.End();
        }
    }
}
